package com.example.demo.controller;

import cn.hutool.crypto.SecureUtil;
import com.example.demo.mapper.AdminMapper;
import com.example.demo.pojo.Admin;
import com.example.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin /*允许跨域访问*/
@RestController
public class AdminController {
    @Autowired
    private AdminMapper adminMapper;
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin) {
        Admin admin1 = adminMapper.login(admin.getUsername());
        String pass = SecureUtil.md5(admin.getPassword());
        System.out.println(admin1);
        System.out.println(pass);
        if (admin1 != null && admin1.getPassword().equals(pass))
            return new Result(200, "登录成功", admin1);
        else if (admin1 == null) {
            return new Result(400, "用户不存在", null);
        }
        // 添加默认返回情况
        return new Result(401, "密码错误", null);
    }
}
